﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SqliteWrapper;
using SqliteWrapper.Query;
using SqliteWrapper.Procedures;
using System.Data;
using BartonExtension;

public partial class iReaderMMS : System.Web.UI.Page
{
    QueryWrapper query_wrapper = null;
    NonQueryWrapper non_query_wrapper = null;
    List<SqliteError> errorPool = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        Initialize();
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        QueryWrapper wrapper = QueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        
        SqliteError error = (SqliteError)wrapper.GetBooks(null,null,null,null,null,null);
        List<Book> dt = error.Response as List<Book>;
        bookDataGrid.DataSource = dt;
        bookDataGrid.DataBind();
    }

    private void Initialize()
    {
        bookDataGrid.EnableSortingAndPagingCallbacks = false;
        query_wrapper = QueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        non_query_wrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        errorPool = new List<SqliteError>();

    }

    protected DataTable QueryAlbums()
    {
        SqliteError error = (SqliteError)query_wrapper.GetAlbums(null, null, null, null, null,null);
        return error.Response as DataTable;
    }

    protected DataTable QueryCategories()
    {
        SqliteError error = (SqliteError)query_wrapper.GetCategories(null, null, null, null, null,null);
        return error.Response as DataTable;
    }

    protected void bookDataGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        bookDataGrid.EditIndex = e.NewEditIndex;
        bookDataGrid.DataBind();
    }

    protected void bookDataGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        bookDataGrid.EditIndex = -1;
        bookDataGrid.DataBind();
    }
    protected void bookDataGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string bkID = e.Values["BookID"].ToString();
        SqliteError err = non_query_wrapper.DeleteBook(Int64.Parse(bkID));
        if (!err.IsProcessSuccess)
            errorPool.Add(err);
        bookDataGrid.EditIndex = -1;
        bookDataGrid.DataBind();
    }
    protected void bookDataGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        bookDataGrid.EditIndex = -1;
        bookDataGrid.DataBind();
    }
    protected void bookDataGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }

    protected void CheckAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox checkall = (CheckBox)sender;
        if (checkall.Checked)
        {
            for (int i = 0; i < bookDataGrid.Rows.Count; i++)
            {
                GridViewRow row = bookDataGrid.Rows[i];
                CheckBox checkbox = (CheckBox)row.FindControl("opCheck");
                checkbox.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < bookDataGrid.Rows.Count; i++)
            {
                GridViewRow row = bookDataGrid.Rows[i];
                ((CheckBox)row.FindControl("opCheck")).Checked = false;
            }
        }
    }

    protected void opCheck_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox theCheck = (CheckBox)sender;
        CheckBox allCheck = (CheckBox)bookDataGrid.HeaderRow.FindControl("allCheck");
        if (!theCheck.Checked)
        {
            allCheck.Checked = false;
        }
        else
        {
            allCheck.Checked = IsAllChecked();
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        List<GridViewRow> checkedList = CheckedRows();
        foreach (GridViewRow row in checkedList)
        {
            bookDataGrid.EditIndex = row.RowIndex;
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        List<GridViewRow> checkedList = CheckedRows();
        foreach (GridViewRow row in checkedList)
        {
            string bkID = row.Cells[1].Text;
            non_query_wrapper.DeleteBook(Convert.ToInt64(bkID));
        }
        BindData();
    }

    private List<GridViewRow> CheckedRows()
    {
        List<GridViewRow> checkedRows = new List<GridViewRow>();
        foreach (GridViewRow row in bookDataGrid.Rows)
        {
            if (((CheckBox)row.FindControl("opCheck")).Checked)
            {
                checkedRows.Add(row);
            }
        }
        return checkedRows;
    }

    private bool IsAllChecked()
    {
        bool checkStatus = true;
        foreach (GridViewRow row in bookDataGrid.Rows)
        {
            if (!((CheckBox)row.FindControl("opCheck")).Checked)
            {
                checkStatus = false;
                break;
            }
        }
        return checkStatus;
    }
}